/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package action;

import beans.ClassFacadeLocal;
import beans.ExamsFacadeLocal;
import beans.SubjectMatterFacadeLocal;
import com.opensymphony.xwork2.ActionSupport;
import entities.Exams;
import entities.SubjectMatter;
import entities.User;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.ejb.MessageDrivenContext;
import javax.jms.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import services.PageDetails;

/**
 *
 * @author corneliugosa
 */

public class ScheduleExam extends ActionSupport implements ServletRequestAware, ServletResponseAware {

    @Resource
    private MessageDrivenContext mdc;

    ClassFacadeLocal classFacade = lookupClassFacadeLocal();
    SubjectMatterFacadeLocal subjectMatterFacade = lookupSubjectMatterFacadeLocal();
    ExamsFacadeLocal examsFacade = lookupExamsFacadeLocal();
    
    private HttpServletRequest request;
    private HttpServletResponse response;
    private List<entities.Class> classesList;
    private Set<SubjectMatter> subjectMatterList;
    private String location;
    private String date;
    private float hour;
    private String selCls;
    private String selSubj;
    private entities.Class cls;
    private SubjectMatter subject;
    private String message;

    public String execute() {
        HttpSession session = request.getSession();
        ServletContext context = request.getServletContext();
        
        List<User> userList = (List<User>) context.getAttribute("userList");
        User user = (User) session.getAttribute("user");
        PageDetails pageDetails = (PageDetails) session.getAttribute("pageDetails");
        pageDetails.setButtonPageDisplay("examsP");

        cls = classFacade.findById(Integer.parseInt(selCls));
        subject = subjectMatterFacade.findById(Integer.parseInt(selSubj));

        Exams exam = new Exams();

        exam.setCls(cls);
        exam.setDate(date);
        exam.setHour(hour);
        exam.setId(0);
      //  exam.setLocation(location);
        exam.setProffessor(user.getPerson());
        exam.setStatus(0);
        exam.setSubjectMatter(subject);

        examsFacade.create(exam);
        message = "The exam schedule request has been sent!";

        /*
         Send JMS message.
         */
        
        try {
            Context jndiContext = new InitialContext();
            ConnectionFactory connectionFactory = (ConnectionFactory) jndiContext.lookup("jms/ScheduleExamFactory");
            Queue queue = (Queue) jndiContext.lookup("jms/ScheduleExam");
            Connection connection = connectionFactory.createConnection();

            Session sess = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
            MessageProducer messageProducer = sess.createProducer(queue);

            ObjectMessage message = sess.createObjectMessage();

            message.setObjectProperty("code", 100);
            
            messageProducer.send(message);
            messageProducer.close();
            connection.close();

        } catch (NamingException ex) {
            Logger.getLogger(ScheduleExam.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JMSException ex) {
            ex.printStackTrace();
        }
        
        for(User usr:userList){
            if(usr.getRole().getId()==3){
//                usr.refresh();
            }
        }
//        try {
//            /*
//             Redirect to servlet!
//             */request.getRequestDispatcher("error.jsp").forward(request, response);
//        } catch (ServletException ex) {
//            Logger.getLogger(ScheduleExam.class.getName()).log(Level.SEVERE, null, ex);
//        } catch (IOException ex) {
//            Logger.getLogger(ScheduleExam.class.getName()).log(Level.SEVERE, null, ex);
//        }

        return SUCCESS;
    }  

    public ScheduleExam() {
        classesList = new ArrayList<entities.Class>();
        subjectMatterList = new HashSet<SubjectMatter>();
    }

    public void addClass(entities.Class cls) {
        classesList.add(cls);
    }

    public void addSubjectMatter(SubjectMatter subject) {
        subjectMatterList.add(subject);
    }

    /**
     * @return the classesList
     */
    public List<entities.Class> getClassesList() {
        return classesList;
    }

    /**
     * @param classesList the classesList to set
     */
    public void setClassesList(List<entities.Class> classesList) {
        this.classesList = classesList;
    }

    /**
     * @return the subjectMatterList
     */
    public Set<SubjectMatter> getSubjectMatterList() {
        return subjectMatterList;
    }

    /**
     * @param subjectMatterList the subjectMatterList to set
     */
    public void setSubjectMatterList(Set<SubjectMatter> subjectMatterList) {
        this.subjectMatterList = subjectMatterList;
    }

    /**
     * @return the location
     */
    public String getLocation() {
        return location;
    }

    /**
     * @param location the location to set
     */
    public void setLocation(String location) {
        this.location = location;
    }

    /**
     * @return the date
     */
    public String getDate() {
        return date;
    }

    /**
     * @param date the date to set
     */
    public void setDate(String date) {
        this.date = date;
    }

    public entities.Class getCls() {
        return cls;
    }

    public void setCls(entities.Class cls) {
        this.cls = cls;
    }

    public ExamsFacadeLocal getExamsFacade() {
        return examsFacade;
    }

    public void setExamsFacade(ExamsFacadeLocal examsFacade) {
        this.examsFacade = examsFacade;
    }

    public SubjectMatter getSubject() {
        return subject;
    }

    public void setSubject(SubjectMatter subject) {
        this.subject = subject;
    }

    public float getHour() {
        return hour;
    }

    public void setHour(float hour) {
        this.hour = hour;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public HttpServletRequest getRequest() {
        return request;
    }

    public void setRequest(HttpServletRequest request) {
        this.request = request;
    }

    public String getSelCls() {
        return selCls;
    }

    public void setSelCls(String selCls) {
        this.selCls = selCls;
    }

    public String getSelSubj() {
        return selSubj;
    }

    public void setSelSubj(String selSubj) {
        this.selSubj = selSubj;
    }

    public HttpServletResponse getResponse() {
        return response;
    }

    public void setResponse(HttpServletResponse response) {
        this.response = response;
    }

    public ClassFacadeLocal getClassFacade() {
        return classFacade;
    }

    public void setClassFacade(ClassFacadeLocal classFacade) {
        this.classFacade = classFacade;
    }

    public MessageDrivenContext getMdc() {
        return mdc;
    }

    public void setMdc(MessageDrivenContext mdc) {
        this.mdc = mdc;
    }

    public SubjectMatterFacadeLocal getSubjectMatterFacade() {
        return subjectMatterFacade;
    }

    public void setSubjectMatterFacade(SubjectMatterFacadeLocal subjectMatterFacade) {
        this.subjectMatterFacade = subjectMatterFacade;
    }

    private ExamsFacadeLocal lookupExamsFacadeLocal() {
        try {
            Context c = new InitialContext();
            return (ExamsFacadeLocal) c.lookup("java:global/School/School-ejb/ExamsFacade!beans.ExamsFacadeLocal");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    @Override
    public void setServletRequest(HttpServletRequest request) {
        this.request = request;
    }

    @Override
    public void setServletResponse(HttpServletResponse response) {
        this.response = response;
    }

    private SubjectMatterFacadeLocal lookupSubjectMatterFacadeLocal() {
        try {
            Context c = new InitialContext();
            return (SubjectMatterFacadeLocal) c.lookup("java:global/School/School-ejb/SubjectMatterFacade!beans.SubjectMatterFacadeLocal");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }

    private ClassFacadeLocal lookupClassFacadeLocal() {
        try {
            Context c = new InitialContext();
            return (ClassFacadeLocal) c.lookup("java:global/School/School-ejb/ClassFacade!beans.ClassFacadeLocal");
        } catch (NamingException ne) {
            Logger.getLogger(getClass().getName()).log(Level.SEVERE, "exception caught", ne);
            throw new RuntimeException(ne);
        }
    }
}
